package o;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.hms.framework.common.ContainerUtils;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class baa extends azz {
    private static final List<String> ˏ = new ArrayList();

    static {
        ˏ.add("serviceType=");
        ˏ.add("sign=");
        ˏ.add("hcrId=");
        ˏ.add("devicetype=");
        ˏ.add("clientPackage=");
        ˏ.add("thirdId=");
        ˏ.add("token=");
        ˏ.add("iv=");
        ˏ.add("terminalType=");
        ˏ.add("userId=");
    }

    public static String ˊ(String str, Map<String, String> map) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        try {
            Map ˋ = ˋ(str);
            if (ˋ != null) {
                Set<Entry> entrySet = ˋ.entrySet();
                if (entrySet != null && entrySet.size() > 0) {
                    for (Entry entry : entrySet) {
                        if (!map.containsKey(entry.getKey())) {
                            map.put(entry.getKey(), entry.getValue());
                        }
                    }
                }
            }
            String[] strArr = (String[]) map.keySet().toArray(new String[map.keySet().size()]);
            int length = strArr.length;
            StringBuffer stringBuffer = new StringBuffer();
            URL url = new URL(str);
            String query = url.getQuery();
            if (query != null || length > 0) {
                stringBuffer.append("?");
            }
            for (int i = 0; i < length; i++) {
                String str2 = strArr[i];
                stringBuffer.append(str2).append('=').append((String) map.get(str2));
                if (i != length - 1) {
                    stringBuffer.append('&');
                }
            }
            String ref = url.getRef();
            if (ref != null) {
                stringBuffer.append("#");
                stringBuffer.append(ref);
            }
            return ˊ(str, query, ref, stringBuffer);
        } catch (Throwable e) {
            azh.ˏ.ˊ("WapParamCreator", "createWapUrl, e: ", e);
            return str;
        }
    }

    private static String ˊ(String str, String str2, String str3, StringBuffer stringBuffer) {
        int indexOf;
        if (str2 != null) {
            indexOf = str.indexOf("?" + str2);
            if (-1 != indexOf) {
                str = str.substring(0, indexOf);
            }
        } else if (str3 != null) {
            indexOf = str.indexOf("#" + str3);
            if (-1 != indexOf) {
                str = str.substring(0, indexOf);
            }
        }
        if (stringBuffer.length() > 0) {
            return str + stringBuffer.toString();
        }
        return str;
    }

    public static Map<String, String> ˋ(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            Object query = new URL(str).getQuery();
            if (TextUtils.isEmpty(query)) {
                return null;
            }
            String[] split = query.split(ContainerUtils.FIELD_DELIMITER);
            if (split.length <= 0) {
                return null;
            }
            Map<String, String> hashMap = new HashMap();
            for (String split2 : split) {
                String[] split3 = split2.split(ContainerUtils.KEY_VALUE_DELIMITER);
                if (split3.length <= 0 || split3.length > 2) {
                    azh.ˏ.ˊ("WapParamCreator", "getParamsFromUrl, result error, param");
                } else {
                    Object obj = "";
                    if (split3.length == 2) {
                        obj = split3[1];
                    }
                    hashMap.put(split3[0], obj);
                }
            }
            return hashMap;
        } catch (Exception e) {
            azh.ˏ.ˏ("WapParamCreator", "getParamsFromUrl URL parse error");
            return null;
        }
    }

    private static boolean ˎ(String str) {
        for (String startsWith : ˏ) {
            if (str.startsWith(startsWith)) {
                return true;
            }
        }
        return false;
    }

    public static String ˏ(String str) {
        String str2 = null;
        int i = 0;
        if (str == null) {
            return null;
        }
        String substring;
        int indexOf = str.indexOf("?");
        if (indexOf > 0) {
            substring = str.substring(0, indexOf);
            str2 = str.substring(indexOf + 1);
        } else {
            substring = str;
        }
        if (str2 == null) {
            return str;
        }
        String[] split = str2.split(ContainerUtils.FIELD_DELIMITER);
        List arrayList = new ArrayList();
        int i2 = 0;
        while (i2 < split.length) {
            if (!(split[i2] == null || split[i2].equals("") || ˎ(split[i2]))) {
                arrayList.add(split[i2]);
            }
            i2++;
        }
        StringBuilder stringBuilder = new StringBuilder(substring);
        int size = arrayList.size();
        if (size > 0) {
            stringBuilder.append("?");
            while (i < size) {
                stringBuilder.append((String) arrayList.get(i));
                if (i != size - 1) {
                    stringBuilder.append('&');
                }
                i++;
            }
        }
        return stringBuilder.toString();
    }

    public String ॱ(Context context) {
        Map ˋ = ˋ(context);
        StringBuilder stringBuilder = new StringBuilder("&dumyParam=1?");
        Object obj = 1;
        for (Entry entry : ˋ.entrySet()) {
            Object obj2;
            if (obj != null) {
                stringBuilder.append((String) entry.getKey()).append('=').append((String) entry.getValue());
                obj2 = null;
            } else {
                stringBuilder.append('&').append((String) entry.getKey()).append('=').append((String) entry.getValue());
                obj2 = obj;
            }
            obj = obj2;
        }
        return stringBuilder.toString();
    }

    public Map<String, String> ˋ(Context context) {
        Map hashMap = new HashMap();
        ˏ(context, hashMap);
        hashMap.put("devicetype", ˎ());
        ˏ(hashMap);
        return hashMap;
    }

    private void ˏ(Map<String, String> map) {
        byte[] ˋ = evh.ˋ();
        map.put("iv", ॱ(ˋ));
        map.put("token", ˎ(ˋ));
        map.put("hcrId", ˊ());
        map.put("deviceId", ˊ(ˋ));
    }

    private String ॱ(byte[] bArr) {
        return dof.ˏ(dnk.ˏ(bArr));
    }

    private String ˎ(byte[] bArr) {
        String str = "";
        UserSession instance = UserSession.getInstance();
        epr ˎ = epr.ˎ();
        if (!(instance == null || ˎ == null || TextUtils.isEmpty(instance.getServiceToken()))) {
            try {
                str = dof.ˏ(eux.ॱ(instance.getServiceToken(), ˎ.ʽ().getBytes("UTF-8"), bArr));
            } catch (UnsupportedEncodingException e) {
                azh.ˏ.ˊ("WapParamCreator", "getToken, UnsupportedEncodingException");
            } catch (Exception e2) {
                azh.ˏ.ˊ("WapParamCreator", "getToken, Exception");
            }
        }
        return str;
    }

    private String ˊ() {
        String str = "";
        HcridSession ˋ = HcridSession.ˋ();
        if (ˋ != null) {
            return ˋ.ॱ();
        }
        return str;
    }

    private String ˊ(byte[] bArr) {
        String str = "";
        try {
            str = eux.ॱ(UserSession.getInstance().getDeviceId(), epr.ˎ().ʽ(), bArr);
        } catch (Exception e) {
            azh.ˏ.ˏ("WapParamCreator", "getUserId, Exception.");
        }
        return str;
    }

    private String ˎ() {
        return UserSession.getInstance().getDeviceType();
    }
}
